from datetime import datetime
import logging
import sys
def printSelf(value, var_name):
    print(f"{var_name} is: "+str(value))




class UTF8FileHandler(logging.FileHandler):
    def __init__(self, filename, mode='a', encoding=None, delay=False):
        # 确保编码被设置为 UTF-8
        if encoding is None:
            encoding = 'utf-8'
        super().__init__(filename, mode, encoding, delay)


# 配置日志记录
logging.basicConfig(
    #filename='nohup.out',  # 指定日志文件名
    level=logging.INFO,    # 设置日志级别为 INFO
    format='%(asctime)s %(levelname)s %(message)s',  # 日志格式
    datefmt='%H%M%S'       # 自定义日期格式，只保留小时分钟秒
)
# 移除默认的 StreamHandler（如果有的话）
root_logger = logging.getLogger()
for handler in root_logger.handlers[:]:
    root_logger.removeHandler(handler)

# 添加自定义的 UTF8FileHandler
log_file_handler = UTF8FileHandler(filename='nohup.out')
root_logger.addHandler(log_file_handler)

def log_info(*args, **kwargs):
    """
    记录信息到日志中。

    :param args: 任意数量的位置参数
    :param kwargs: 任意数量的关键字参数
    """
    # 构建日志消息
    message_parts = []

    # 添加位置参数到日志消息
    for arg in args:
        message_parts.append(str(arg))

    # 添加关键字参数到日志消息
    for key, value in kwargs.items():
        message_parts.append(f'{key}={value}')

    # 将所有部分连接成一个完整的日志消息
    timeObj=datetime.now()
    timeStr=add0(timeObj.hour)+":"+add0(timeObj.minute)+":"+add0(timeObj.second)
    log_message = ' '.join(message_parts)

    # 记录日志
    logging.info(timeStr+" "+log_message)

def add0(n):
    if n<10:
        return "0"+str(n)
    else:
        return str(n)

def withColorPrint(text,color):
    newText=color+str(text)+"\033[0m"
    return newText

def redPrint(text):
    newText=withColorPrint(text,'\033[0;31m')
    return newText

def bluePrint(text):
    newText=withColorPrint(text,'\033[0;34m')
    return newText